package Forms;

import Controls.*;
import Core.*;

public class ColorPallete extends Form
{
	private int color, cancelColor;
	private Slider red, green, blue;
	private ColorSelector colorSelector;
	private Control label[] = new Control[3];
	static final private int colors[] = {0x990030, 0xed1c24, 0xffc20e, 0xa8e61d, 0x22b14c, 0x00b7ef, 0x2f3699, 0x6f3198};

	public ColorPallete(int c)
	{
		super(Form.HEAD | Form.THIN_PANEL);

		body.h_border = 3;

		setWindowText(GM.FORM_CAPTION, "Палитра");
		setWindowText(GM.PANEL_LEFTSOFT, "Отмена");
		setWindowText(GM.PANEL_RIGHTSOFT, "Ok");

		label[0] = new Text("Красный: ");
		red = (Slider) body.addControl(new Slider(body.getAvailableW()));
		body.setHotControl(red, 1);

		label[1] = new Text("Зеленый: ");
		green = (Slider) body.addControl(new Slider(body.getAvailableW()));
		body.setHotControl(green, 2);

		label[2] = new Text("Синий: ");
		blue = (Slider) body.addControl(new Slider(body.getAvailableW()));
		body.setHotControl(blue, 3);
		
		LayoutManager row = new LayoutManager(body.getAvailableW(), 20);
		row.type = LayoutManager.HORIZONTAL;
		row.setSelectable(false);
		row.addControl(new Spacer());
		colorSelector = (ColorSelector) row.addControl(new ColorSelector(c, 20, false));
		row.addControl(new Spacer());
		body.addControl(row);
		body.select(Control.UNDER);

		cancelColor = c;
		setColor(c);

	}

	public void setColor(int color)
	{
		this.color = color;

		red.setPosition((double)(color >> 16)/(double)255);
		green.setPosition((double)((color >> 8) & 0xff)/(double)255);
		blue.setPosition((double)(color & 0xff)/(double)255);

		colorSelector.setColor(color);
	}

	public int getColor()
	{
		return color;
	}

	public void onEvent(int event, Object param)
	{
		super.onEvent(event, param);

		if(event == GM.SLIDER_MOVED)
		{
			setColor(((int)(red.getPosition()*255) << 16) + ((int)(green.getPosition()*255) << 8) + (int)(blue.getPosition()*255));
		}

		if(event == GM.PANEL_LEFTSOFT)
		{
			color = cancelColor;
			close();
		}

		if(event == GM.PANEL_RIGHTSOFT)
		{
			close();
		}

		if(event == GM.CONTROL_SELECTED)
		{
			for(int i=0; i<3; i++) body.removeControl(label[i]);

			if(param == red) body.insertControlBefore(label[0], red);
			if(param == green) body.insertControlBefore(label[1], green);
			if(param == blue) body.insertControlBefore(label[2], blue);
		}
	}
}
